package com.songqintuan.mall.customer;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import net.sf.cglib.beans.BeanCopier;

import com.songqintuan.common.bean.GenerateVerifyCodeBean;
import com.songqintuan.common.util.DESPlusUtil;
import com.songqintuan.common.util.MessageFactory;
import com.songqintuan.common.util.SpringFacesUtil;
import com.songqintuan.mall.constant.MallConstants;
import com.songqintuan.mall.constant.MallManageBeanConstants;
import com.songqintuan.mall.constant.MallSpringBeanConstants;
import com.songqintuan.model.po.Customer;
import com.songqintuan.model.vo.CustomerVo;
import com.songqintuan.persist.service.hibernate.ICustomerService;



@ManagedBean(name="createCustomerPage")
@ViewScoped
public class CreateCustomerPageBean implements java.io.Serializable {
	private static final long serialVersionUID = -8106629465344224753L;
	private CustomerVo customer = new CustomerVo();
	private List<String> errorMessages = new ArrayList<String>();
	private boolean agreeFlag = false;
	private String verifyCodeForQuestion;
	private String verifyCode;
	private String verifyPsssword;
	
	public CustomerVo getCustomer() {
		return customer;
	}

	public void setCustomer(CustomerVo customer) {
		this.customer = customer;
	}
	
	public List<String> getErrorMessages() {
		return errorMessages;
	}

	public void setErrorMessages(List<String> errorMessages) {
		this.errorMessages = errorMessages;
	}

	public boolean isAgreeFlag() {
		return agreeFlag;
	}

	public void setAgreeFlag(boolean agreeFlag) {
		this.agreeFlag = agreeFlag;
	}

	public String getVerifyCodeForQuestion() {
		return verifyCodeForQuestion;
	}

	public void setVerifyCodeForQuestion(String verifyCodeForQuestion) {
		this.verifyCodeForQuestion = verifyCodeForQuestion;
	}

	public String getVerifyCode() {
		return verifyCode;
	}

	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}

	public String getVerifyPsssword() {
		return verifyPsssword;
	}

	public void setVerifyPsssword(String verifyPsssword) {
		this.verifyPsssword = verifyPsssword;
	}

	public CreateCustomerPageBean(){
		// GenerateVerifyCode
		GenerateVerifyCodeBean generateVerifyCode = (GenerateVerifyCodeBean) SpringFacesUtil.getManagedBean(MallManageBeanConstants.GENERATE_VERIFY_CODE_BEAN);
		this.setVerifyCodeForQuestion(generateVerifyCode.generateCode());
		// end generateVerifyCode
	}
	public String generateVerifyCode() {
		GenerateVerifyCodeBean generateVerifyCode = (GenerateVerifyCodeBean) SpringFacesUtil.getManagedBean(MallManageBeanConstants.GENERATE_VERIFY_CODE_BEAN);
		this.setVerifyCodeForQuestion(generateVerifyCode.generateCode());
		return null;
	}
	public String saveCustomer() {
		this.getErrorMessages().clear();
		ICustomerService<Customer,String> objService = (ICustomerService<Customer,String>) SpringFacesUtil.getSpringBean(MallSpringBeanConstants.CUSTOMER_SERVICE_BEAN_NAME);
		objService.setEntityClass(Customer.class);
		objService.setPKClass(String.class);
		Customer newObj = new Customer();
		BeanCopier copyHere = BeanCopier.create(CustomerVo.class,Customer.class,false);
		copyHere.copy(this.getCustomer(), newObj, null);
		Customer obj = objService.findUniqueBy("loginName", this.getCustomer().getLoginName());
		
		if (null != obj) {
			this.getErrorMessages().add(MessageFactory.getMessage(MallConstants.USER_BUNDLE_NAME,"customer.has.exist.error.message", FacesMessage.SEVERITY_ERROR, this.getCustomer().getLoginName()).getDetail());
		}

		String password = (String) this.getCustomer().getLoginPassword();
		String verifyPassword = (null == this.getVerifyPsssword())?"":this.getVerifyPsssword();
		if (!(password.equals(verifyPassword))) {
			this.getErrorMessages().add(MessageFactory.getMessage(MallConstants.USER_BUNDLE_NAME,"password.notmatch.error.message", FacesMessage.SEVERITY_ERROR).getDetail());
		}
		if (!(this.getVerifyCodeForQuestion().toUpperCase().equals(this.getVerifyCode().toUpperCase()))) {
			this.getErrorMessages().add(MessageFactory.getMessage(MallConstants.USER_BUNDLE_NAME,"verifycode.notmatch.error.message", FacesMessage.SEVERITY_ERROR).getDetail());
		}
		if (this.getErrorMessages().size() > 0) {
			return null;
		}
		String enPassword = DESPlusUtil.encrypt(password);
		newObj.setLoginPassword(enPassword);
		newObj.setEmail(this.getCustomer().getLoginName());
		newObj.setStatus(MallConstants.CUSTOMER_STATUS_ACTIVE);
		objService.save(newObj);
		//re generate verifycode
		GenerateVerifyCodeBean generateVerifyCode = (GenerateVerifyCodeBean) SpringFacesUtil.getManagedBean(MallManageBeanConstants.GENERATE_VERIFY_CODE_BEAN);
		this.setVerifyCodeForQuestion(generateVerifyCode.generateCode());
		//end
		String tempCustomerId = this.getCustomer().getLoginName();
		//clear data for input
		this.setVerifyCode("");
		this.setAgreeFlag(false);
		//end
		return "registerSuccess" + "?faces-redirect=true&loginName=" + tempCustomerId + "&param=" + enPassword;
	}
}
